package cn.edu.nuc.ssm.controller;

import cn.edu.nuc.ssm.dto.DoListSearchTimeParam;
import cn.edu.nuc.ssm.dto.ShouruParam;
import cn.edu.nuc.ssm.dto.ZhichuParam;
import cn.edu.nuc.ssm.entity.*;
import cn.edu.nuc.ssm.service.AccountService;
import cn.edu.nuc.ssm.service.DoListService;
import cn.edu.nuc.ssm.service.JizhangService;
import cn.edu.nuc.ssm.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2017/4/6.
 */
@Controller
@RequestMapping("/jizhang")
public class JizhangController {
    private static Logger logger = LoggerFactory.getLogger(JizhangController.class);

    @Autowired
    private JizhangService jizhangService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private DoListService doListService;





    //进入记账的首页
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(Model model,HttpSession session){

        User user = (User) session.getAttribute("user");
        if (user==null){
            return "redirect:/front/tologin";
        }
        List<Category> blongZhicu = jizhangService.findBlongZhicuCategory(user.getId());
        List<Category> list = jizhangService.findBlongSouruCategory(user.getId());
        List<Account> userAccountList = jizhangService.findByUid(user.getId());

        model.addAttribute("type_zichu_list",blongZhicu);
        model.addAttribute("type_souru_list",list);
        model.addAttribute("userAccountList",userAccountList);

        model.addAttribute("start", DateUtil.getCurrentMouthFirstDay());
        model.addAttribute("end", DateUtil.getCurrentMouthLastDay());
        return "front/jizhang";
    }
    //支出记账的一系统操作
    @ResponseBody
    @RequestMapping(value = "/zhicu",method = RequestMethod.POST)
  //  public String zhichu(PayIncome payIncome,@RequestParam("acc_id") int acc_id,Model model,HttpSession session){
    public String zhichu(@RequestBody ZhichuParam zhichuParam, Model model, HttpSession session){

        logger.debug(zhichuParam.toString());

        PayIncome payIncome = zhichuParam.getPayIncome();
        int acc_id = zhichuParam.getAcc_id();

        //当前的操作的用户id
        User user = (User) session.getAttribute("user");


        if (user==null){
            return "redirect:/front/tologin";
        }


        payIncome.setUid(user.getId());
        try {
            payIncome.setDate( payIncome.getDate().substring(0,10) );
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        Account account = accountService.findbyId(acc_id);
        logger.debug("查找的账户的信息是:{}",account);
        int i = 0;
        try {
            i = jizhangService.zhicuMoney(payIncome, account);
        } catch (Exception e) {
            e.printStackTrace();
            if(e.getMessage().equals("账户余额不足")){

                return "moneyShort";
            }


            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        if (i<=0){
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        return "success";
    }

    //收入记账的一系统操作
    @ResponseBody
    @RequestMapping(value = "/shouru",method = RequestMethod.POST)
    //  public String zhichu(PayIncome payIncome,@RequestParam("acc_id") int acc_id,Model model,HttpSession session){
    public String shouru(@RequestBody ShouruParam shouruParam, Model model, HttpSession session){

        logger.debug(shouruParam.toString());

        PayIncome payIncome = shouruParam.getPayIncome();
        int acc_id = shouruParam.getAcc_id();

        //当前的操作的用户id
        User user = (User) session.getAttribute("user");


        if (user==null){
            return "redirect:/front/tologin";
        }
        payIncome.setUid(user.getId());
        try {
            payIncome.setDate( payIncome.getDate().substring(0,10) );

        } catch (Exception e) {

            e.printStackTrace();
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        Account account = accountService.findbyId(acc_id);
        logger.debug("查找的账户的信息是:{}",account);
        int i = 0;
        try {
            i = jizhangService.shouruMoney(payIncome,account);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        if (i<=0){
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        return "success";
    }

    //转账的一系统操作
    @ResponseBody
    @RequestMapping(value = "/zhuangzhang",method = RequestMethod.POST)
    //  public String zhichu(PayIncome payIncome,@RequestParam("acc_id") int acc_id,Model model,HttpSession session){
    public String zhuangzhang(@RequestBody AccountTransfer accountTransfer, Model model, HttpSession session){

        logger.debug("请求的参数是:{}",accountTransfer.toString());

        if(accountTransfer.getAccountInId().equals(accountTransfer.getAccountOutId())){

            model.addAttribute("msg","数据发生错误");
            return "accError";
        }

        //当前的操作的用户id
        User user = (User) session.getAttribute("user");

        if (user==null){
            return "redirect:/front/tologin";
        }
        accountTransfer.setUid(user.getId());
        try {
            accountTransfer.setTransferDate( accountTransfer.getTransferDate().substring(0,10) );

        } catch (Exception e) {

            e.printStackTrace();
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        int i=0;
        try {
             i = jizhangService.zhuanZhangMoney(accountTransfer);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg","数据发生错误");
            if("账户余额不足".equals(e.getMessage())){

                return "yuError";
            }


            return "error";
        }
        if (i<=0){
            model.addAttribute("msg","数据发生错误");
            return "error";
        }

        return "success";
    }

    //借入的的一系统操作
    @ResponseBody
    @RequestMapping(value = "/jianru",method = RequestMethod.POST)
    public String jiankuan(@RequestBody AccountTransfer accountTransfer, Model model, HttpSession session){

        logger.debug("请求的参数是:{}",accountTransfer.toString());

        if(accountTransfer.getAccountInId().equals(accountTransfer.getAccountOutId())){

            model.addAttribute("msg","数据发生错误");
            return "error";
        }

        //当前的操作的用户id
        User user = (User) session.getAttribute("user");

        if (user==null){
            return "redirect:/front/tologin";
        }
        accountTransfer.setUid(user.getId());
        try {
            accountTransfer.setTransferDate( accountTransfer.getTransferDate().substring(0,10) );

        } catch (Exception e) {

            e.printStackTrace();
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        int i=0;
        try {
            i = jizhangService.jianruMoney(accountTransfer);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        if (i<=0){
            model.addAttribute("msg","数据发生错误");
            return "error";
        }

        return "success";
    }

    //借出的的一系统操作
    @ResponseBody
    @RequestMapping(value = "/jiancu",method = RequestMethod.POST)
    public String jiancu(@RequestBody AccountTransfer accountTransfer, Model model, HttpSession session){

        logger.debug("请求的参数是:{}",accountTransfer.toString());

        if(accountTransfer.getAccountInId().equals(accountTransfer.getAccountOutId())){

            model.addAttribute("msg","数据发生错误");
            return "error";
        }

        //当前的操作的用户id
        User user = (User) session.getAttribute("user");

        if (user==null){
            return "redirect:/front/tologin";
        }
        accountTransfer.setUid(user.getId());
        try {
            accountTransfer.setTransferDate( accountTransfer.getTransferDate().substring(0,10) );

        } catch (Exception e) {

            e.printStackTrace();

            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        int i=0;
        try {
            i = jizhangService.jiancuMoney(accountTransfer);
        } catch (Exception e) {
            e.printStackTrace();
            if(e.getMessage().equals("账户余额不足")){

                return "moneyShort";
            }
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        if (i<=0){
            model.addAttribute("msg","数据发生错误");
            return "error";
        }

        return "success";
    }

    //还款的的一系统操作
    @ResponseBody
    @RequestMapping(value = "/shouzai",method = RequestMethod.POST)
    public String shouzai(@RequestBody AccountTransfer accountTransfer, Model model, HttpSession session){

        logger.debug("请求的参数是:{}",accountTransfer.toString());
        if(accountTransfer.getAccountInId().equals(accountTransfer.getAccountOutId())){
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        //当前的操作的用户id
        User user = (User) session.getAttribute("user");

        if (user==null){
            return "redirect:/front/tologin";
        }

        accountTransfer.setUid(user.getId());
        try {
            accountTransfer.setTransferDate( accountTransfer.getTransferDate().substring(0,10) );
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        int i=0;
        try {
            i = jizhangService.shouzaiMoney(accountTransfer);
        } catch (Exception e) {
            if(e.getMessage().equals("收债金额不对")){

                return "moneyError";
            }

            e.printStackTrace();
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        if (i<=0){
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        return "success";
    }


    //还款的的一系统操作
    @ResponseBody
    @RequestMapping(value = "/huankuan",method = RequestMethod.POST)
    public String huankuan(@RequestBody AccountTransfer accountTransfer, Model model, HttpSession session){

        logger.debug("请求的参数是:{}",accountTransfer.toString());

        if(accountTransfer.getAccountInId().equals(accountTransfer.getAccountOutId())){

            model.addAttribute("msg","数据发生错误");
            return "error";
        }

        //当前的操作的用户id
        User user = (User) session.getAttribute("user");

        if (user==null){
            return "redirect:/front/tologin";
        }
        accountTransfer.setUid(user.getId());
        try {
            accountTransfer.setTransferDate( accountTransfer.getTransferDate().substring(0,10) );

        } catch (Exception e) {

            e.printStackTrace();
            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        int i=0;
        try {
            i = jizhangService.huankuanMoney(accountTransfer);
        } catch (Exception e) {
            e.printStackTrace();
            if(e.getMessage().equals("还账金额不对")){

                return "moneyError";
            }

            model.addAttribute("msg","数据发生错误");
            return "error";
        }
        if (i<=0){
            model.addAttribute("msg","数据发生错误");
            return "error";
        }

        return "success";
    }


    @ResponseBody
    @RequestMapping(value = "/do_list_do",method = RequestMethod.POST)
    public List<DoList> listDoList(HttpSession session,@RequestBody DoListSearchTimeParam doListSearchTimeParam){

        logger.debug("请求的参数是:{}",doListSearchTimeParam.toString());

        //当前的操作的用户id
        User user = (User) session.getAttribute("user");
        List<DoList> doLists = doListService.findbyUseIdAndRecordDateBetweenOrEqualTo(user.getId(), doListSearchTimeParam.getStart(), doListSearchTimeParam.getEnd());

        return doLists;
    }


}
